import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import {getuserinfo} from '../api/api'
// import {updateUser} from '../api/api'


export default new Vuex.Store({
  state: {
    //下边的tab的控制键
    active:0,

    //是否登录 否
    isLogin: false,
    data: {userName:''},
    // 搜索记录
    test: [],
    //是否在注册页 否
    Reg:false,
  //  是否忘记密码
    Fog:false
  },
  mutations: {
    changeActive(state,payload){
      state.active=payload.active
    },
    changeLogin(state, payload) {
      state.isLogin = payload.isLogin;
      state.data = payload.data
    },
    changeFog(state,payload){
      state.Fog=payload.Fog;
    },
    changeReg(state,payload){
      state.Reg=payload.reg
    },
    changeremark(state, payload) {
      state.data.remark = payload.remark
    },
    changephonenumber(state, payload) {
      state.data.phonenumber = payload.phonenumber
    },
    changeuserName(state, payload) {
      state.data.userName = payload.userName
    },
    // 添加记录
    addText: function (state, test) {
      state.test.push(test)
    },
  },
  getters: {
    getTest: function (state) {
      return state.test;
    },
    changeuserName(state,payload){
      state.data.userName=payload.userName
    },
    changesex(state,payload){
      state.data.sex=payload.sex
    },
    changeAvatar(state,payload){
      payload.avatar=state.data.avatar
    }
  },
  actions: {
    // 使登录状态不变
    getLogin(context) {
      getuserinfo().then((res) => {
        // console.log(res)
        if (res.code === 0) {
          context.commit('changeLogin', { isLogin: true, data: res.data })
        } else {
          context.commit('changeLogin', { isLogin: false, data: {} })

        }
      })

    },
    saveTest({commit},sta){
      commit('addText', sta)
    },
    // //  使改变的信息不变
    //   getremark(context,payload){
    //     updateUser().then((res)=>{
    //       if(res.code===0){
    //         context.commit('changeremark',{remark:payload.remark})
    //       }
    //
    //     })
    //     // context.commit('changeremark',{remark:})
    //   }
  },
  modules: {
  }
})
